package com.zhentao.util;

/**
 * @Date 2025/4/21 19:25
 * @Author gln
 **/

public class SnowflakeUtil {//雪花算法
    // 起始时间戳，可自定义，这里以 2020-01-01 00:00:00 为例
    private static final long START_TIMESTAMP = 1577836800000L;

    // 数据中心 ID 所占位数
    private static final long DATA_CENTER_ID_BITS = 5L;
    // 机器 ID 所占位数
    private static final long WORKER_ID_BITS = 5L;
    // 序列号所占位数
    private static final long SEQUENCE_BITS = 12L;

    // 数据中心 ID 最大值
    private static final long MAX_DATA_CENTER_ID = -1L ^ (-1L << DATA_CENTER_ID_BITS);
    // 机器 ID 最大值
    private static final long MAX_WORKER_ID = -1L ^ (-1L << WORKER_ID_BITS);
    // 序列号最大值
    private static final long SEQUENCE_MASK = -1L ^ (-1L << SEQUENCE_BITS);

    // 机器 ID 向左移位数
    private static final long WORKER_ID_SHIFT = SEQUENCE_BITS;
    // 数据中心 ID 向左移位数
    private static final long DATA_CENTER_ID_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS;
    // 时间戳向左移位数
    private static final long TIMESTAMP_LEFT_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS + DATA_CENTER_ID_BITS;

    // 固定数据中心 ID 和机器 ID
    private static final long DATA_CENTER_ID = 1;
    private static final long WORKER_ID = 1;

    private static long sequence = 0L;
    private static long lastTimestamp = -1L;

    public static synchronized Long nextId() {
        long timestamp = System.currentTimeMillis();

        if (timestamp < lastTimestamp) {
            throw new RuntimeException("Clock moved backwards. Refusing to generate id for " + (lastTimestamp - timestamp) + " milliseconds");
        }

        if (timestamp == lastTimestamp) {
            sequence = (sequence + 1) & SEQUENCE_MASK;
            if (sequence == 0) {
                timestamp = waitForNextMillis(lastTimestamp);
            }
        } else {
            sequence = 0L;
        }

        lastTimestamp = timestamp;

        return ((timestamp - START_TIMESTAMP) << TIMESTAMP_LEFT_SHIFT) |
                (DATA_CENTER_ID << DATA_CENTER_ID_SHIFT) |
                (WORKER_ID << WORKER_ID_SHIFT) |
                sequence;
    }

    private static long waitForNextMillis(long lastTimestamp) {
        long timestamp = System.currentTimeMillis();
        while (timestamp <= lastTimestamp) {
            timestamp = System.currentTimeMillis();
        }
        return timestamp;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(nextId());
        }
    }
}

